{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sqlalchemy.ext.automap import automap_base\n",
    "from sqlalchemy.orm import Session\n",
    "from sqlalchemy import create_engine\n",
    "\n",
    "Base = automap_base()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from sqlalchemy import create_engine\n",
    "\n",
    "engine = create_engine('sqlite:///Chinook_Sqlite.sqlite')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Base.prepare(engine, reflect=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['Album',\n",
       " 'Customer',\n",
       " 'Playlist',\n",
       " 'Artist',\n",
       " 'Track',\n",
       " 'Employee',\n",
       " 'MediaType',\n",
       " 'InvoiceLine',\n",
       " 'Invoice',\n",
       " 'Genre']"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Base.classes.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "Artist = Base.classes.Artist\n",
    "Album = Base.classes.Album"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, u'AC/DC')\n",
      "(2, u'Accept')\n",
      "(3, u'Aerosmith')\n",
      "(4, u'Alanis Morissette')\n",
      "(5, u'Alice In Chains')\n",
      "(6, u'Ant\\xf4nio Carlos Jobim')\n",
      "(7, u'Apocalyptica')\n",
      "(8, u'Audioslave')\n",
      "(9, u'BackBeat')\n",
      "(10, u'Billy Cobham')\n"
     ]
    }
   ],
   "source": [
    "from sqlalchemy.orm import Session\n",
    "\n",
    "session = Session(engine)\n",
    "for artist in session.query(Artist).limit(10):\n",
    "    print(artist.ArtistId, artist.Name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "AC/DC - For Those About To Rock We Salute You\n",
      "AC/DC - Let There Be Rock\n"
     ]
    }
   ],
   "source": [
    "artist = session.query(Artist).first()\n",
    "for album in artist.album_collection:\n",
    "    print('{} - {}'.format(artist.Name, album.Title))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
